<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>拓扑结构节点系统</title>
  <link rel="stylesheet" href="CSS/topology.css">
  <script src="JS/svg_line.js"></script>
  <script src="JS/topology.js"></script>
  <script src="JS/topologyRenderer.js"></script>
</head>
<body>
  <div class="main">
    <header>
      <h1>
        拓扑结构节点系统
      </h1>
      <p class="subtitle">
        此组件展示节点之间的父子关系和前后关系，使用带箭头的线条表示连接关系。您可以添加新节点、拖拽节点调整位置，系统会自动维护节点之间的关系。
      </p>
    </header>
    <div class="content">
      <div class="topology-container" id="topology">
        <!-- <div style="width: 100%;height: 100%;" id="topology">
        </div> -->
      </div>
      <div class="controls">
        <div class="legend">
          <h2>图例说明</h2>
          <div class="legend-item">
            <div class="legend-color" style="background: #4facfe;"></div>
            <div>父子关系连接线（实线）</div>
          </div>
          <div class="legend-item">
            <div class="legend-color" style="background: #00f2fe;"></div>
            <div>前后关系连接线（虚线）</div>
          </div>
          <div class="legend-item">
            <div class="legend-color" style="background: #9b59b6;"></div>
            <div>根节点（无父节点）</div>
          </div>
          <div class="legend-item">
            <div class="legend-color" style="background: #2ecc71;"></div>
            <div>中间节点（有父节点和子节点）</div>
          </div>
          <div class="legend-item">
            <div class="legend-color" style="background: #e74c3c;"></div>
            <div>叶节点（无子节点）</div>
          </div>
        </div>
        
        <div class="control-group">
          <h2>节点操作</h2>
          <button class="btn" id="addRoot">添加根节点</button>
          <button class="btn" id="addChild">添加子节点</button>
          <button class="btn" id="addSibling">添加兄弟节点</button>
          <button class="btn" id="removeNode">删除选中节点</button>
        </div>
        
        <div class="control-group">
          <h2>布局操作</h2>
          <button class="btn" id="arrangeLayout">自动排列布局</button>
          <button class="btn" id="reset">重置拓扑结构</button>
        </div>
        
        <div class="instructions">
          <h2>操作指南</h2>
          <ul>
            <li><strong>添加节点</strong>：点击右侧按钮添加根节点、子节点或兄弟节点</li>
            <li><strong>选择节点</strong>：点击节点进行选择（选中的节点会有高亮边框）</li>
            <li><strong>移动节点</strong>：拖拽节点可以调整位置，连接线会自动更新</li>
            <li><strong>删除节点</strong>：选中节点后点击"删除选中节点"</li>
            <li><strong>自动布局</strong>：点击"自动排列布局"按钮优化节点位置</li>
          </ul>
        </div>
      </div>
    </div>
  </div>
  <script>
    const topology = new Topology();
    var topologyRenderer;
    let count=1;
    (function(){
      var container = document.getElementById("topology");
      topologyRenderer = new TopologyRenderer(container,topology,nodeInnerHtml);
      init();
      topologyRenderer.arrangeLayout();

      const btn_add_root = document.getElementById("addRoot");
      btn_add_root.addEventListener("click",(e)=>{
        topology.appNode(undefined,undefined,{name:"节点_new_"+(count++)});
        topologyRenderer.render();
      });
      const btn_addChild = document.getElementById("addChild");
      btn_addChild.addEventListener("click",(e)=>{
        if(!topology.selectedNode) {
          alert("请选择父级节点");
          return;
        }
        topology.appNode(topology.selectedNode,undefined,{name:"节点_new_"+(count++)});
        topologyRenderer.render();
      });
      const btn_addSibling = document.getElementById("addSibling");
      btn_addSibling.addEventListener("click",(e)=>{
        if(!topology.selectedNode) {
          alert("请选择前节点");
          return;
        }
        topology.appNode(undefined,topology.selectedNode,{name:"节点_new_"+(count++)});
        topologyRenderer.render();
      });
      const btn_removeNode = document.getElementById("removeNode");
      btn_removeNode.addEventListener("click",(e)=>{
        if(!topology.selectedNode) {
          alert("请选择要删除的节点");
          return;
        }
        topology.removNode(topology.selectedNode);
        topologyRenderer.render();
      });
      const btn_arrangeLayout = document.getElementById("arrangeLayout");
      btn_arrangeLayout.addEventListener("click",(e)=>{
        topologyRenderer.arrangeLayout();
      });
      const btn_reset = document.getElementById("reset");
      btn_reset.addEventListener("click",(e)=>{
        topologyRenderer.topology.clearNode();
        topologyRenderer.render();
      });
    })();

    function init(){
      var rootNode1 = topology.appNode(undefined,undefined,{name:"根节点1"});
      var node1_1 = topology.appNode(rootNode1.id,undefined,{name:"子节点1_1"});
      var node1_1_next_1 = topology.appNode(undefined,node1_1.id,{name:"子节点1_1_next_1"});
      var node1_1_next_2 = topology.appNode(undefined,node1_1_next_1.id,{name:"子节点1_1_next_2"});
      var node1_2 = topology.appNode(rootNode1.id,undefined,{name:"子节点1_2"});
      var node1_3 = topology.appNode(rootNode1.id,undefined,{name:"子节点1_3"});
      
      var node1_1_next_1_1 = topology.appNode(node1_1_next_1.id,undefined,{name:"子节点1_1_next_1_子1"});

      var node1_1_next_1_2 = topology.appNode(node1_1_next_1.id,undefined,{name:"子节点1_1_next_1_子2"});
      var node1_1_next_1_3 = topology.appNode(node1_1_next_1.id,undefined,{name:"子节点1_1_next_1_子3"});
      
      var node1_1_next_1_1_1 = topology.appNode(undefined,node1_1_next_1_1.id,{name:"子节点1_1_next_1_子1_1"});
      var node1_1_next_1_1_2 = topology.appNode(undefined,node1_1_next_1_1_1.id,{name:"子节点1_1_next_1_子1_2"});
      
      var node1_1_next_2_1 = topology.appNode(node1_1_next_2.id,undefined,{name:"子节点1_1_next_2_子1"});
      var node1_1_next_2_2 = topology.appNode(node1_1_next_2.id,undefined,{name:"子节点1_1_next_2_子2"});
      
      var rootNode2 = topology.appNode(undefined,undefined,{name:"根节点2"});
      var node2_1 = topology.appNode(rootNode2.id,undefined,{name:"子节点2_1"});
      var node2_2 = topology.appNode(rootNode2.id,undefined,{name:"子节点2_2"});
      var node2_3 = topology.appNode(rootNode2.id,undefined,{name:"子节点2_3"});
    }

    function nodeInnerHtml(node){
      return `
        <div class="node-title">${node.data.name}</div>
      `;
    }
  </script>
</body>
</html>